using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using CRM_SMS_MARKETING.EL;

namespace GSM.BusinessLayer.DataLayer
{
	/// <summary>
	/// Data access layer class for UserAccount
	/// </summary>
	class UserAccountSql : DataLayerBase 
	{

        #region Constructor

		/// <summary>
		/// Class constructor
		/// </summary>
		public UserAccountSql()
		{
			// Nothing for now.
		}

        #endregion

        #region Public Methods

        /// <summary>
        /// insert new row in the table
        /// </summary>
		/// <param name="businessObject">business object</param>
		/// <returns>true of successfully insert</returns>
		public bool Insert(UserAccount businessObject)
		{
			SqlCommand	sqlCommand = new SqlCommand();
			sqlCommand.CommandText = "dbo.[UserAccount_Insert]";
			sqlCommand.CommandType = CommandType.StoredProcedure;

			// Use connection object of base class
			sqlCommand.Connection = MainConnection;

			try
			{
                
				sqlCommand.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int, 4, ParameterDirection.Output, false, 0, 0, "", DataRowVersion.Proposed, businessObject.UserID));
				sqlCommand.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.UserName));
				sqlCommand.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Password));
				sqlCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Name));
				sqlCommand.Parameters.Add(new SqlParameter("@Gender", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Gender));
				sqlCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 200, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Address));
				sqlCommand.Parameters.Add(new SqlParameter("@District", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.District));
				sqlCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.City));
				sqlCommand.Parameters.Add(new SqlParameter("@Birthday",businessObject.Birthday));
                //sqlCommand.Parameters.Add(new SqlParameter("@Birthday", SqlDbType.Text, 3, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Birthday));
				sqlCommand.Parameters.Add(new SqlParameter("@HomePhone", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.HomePhone));
				sqlCommand.Parameters.Add(new SqlParameter("@CellPhone", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.CellPhone));
				sqlCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Email));
				sqlCommand.Parameters.Add(new SqlParameter("@UserType", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.UserType));
				sqlCommand.Parameters.Add(new SqlParameter("@Deleted", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Deleted));

								
				MainConnection.Open();
				
				sqlCommand.ExecuteNonQuery();
                businessObject.UserID = (int)sqlCommand.Parameters["@UserID"].Value;

				return true;
			}
			catch(Exception ex)
			{				
				throw new Exception("UserAccount::Insert::Error occured.", ex);
			}
			finally
			{			
				MainConnection.Close();
				sqlCommand.Dispose();
			}
		}

         /// <summary>
        /// update row in the table
        /// </summary>
        /// <param name="businessObject">business object</param>
        /// <returns>true for successfully updated</returns>
        public bool Update(UserAccount businessObject)
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[UserAccount_Update]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {
                
				sqlCommand.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.UserID));
				sqlCommand.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.UserName));
				sqlCommand.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Password));
				sqlCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Name));
				sqlCommand.Parameters.Add(new SqlParameter("@Gender", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Gender));
				sqlCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 200, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Address));
				sqlCommand.Parameters.Add(new SqlParameter("@District", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.District));
				sqlCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.City));
                sqlCommand.Parameters.Add(new SqlParameter("@Birthday", businessObject.Birthday));
				//sqlCommand.Parameters.Add(new SqlParameter("@Birthday", SqlDbType.Text, 3, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Birthday));
				sqlCommand.Parameters.Add(new SqlParameter("@HomePhone", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.HomePhone));
				sqlCommand.Parameters.Add(new SqlParameter("@CellPhone", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.CellPhone));
				sqlCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Email));
				sqlCommand.Parameters.Add(new SqlParameter("@UserType", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.UserType));
				sqlCommand.Parameters.Add(new SqlParameter("@Deleted", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Deleted));

                
                MainConnection.Open();

                sqlCommand.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("UserAccount::Update::Error occured.", ex);
            }
            finally
            {
                MainConnection.Close();
                sqlCommand.Dispose();
            }
        }

        /// <summary>
        /// Select by primary key
        /// </summary>
        /// <param name="keys">primary keys</param>
        /// <returns>UserAccount business object</returns>
        public UserAccount SelectByPrimaryKey(UserAccountKeys keys)
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[UserAccount_SelectByPrimaryKey]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

				sqlCommand.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, keys.UserID));

                
                MainConnection.Open();

                IDataReader dataReader = sqlCommand.ExecuteReader();

                if (dataReader.Read())
                {
                    UserAccount businessObject = new UserAccount();

                    PopulateBusinessObjectFromReader(businessObject, dataReader);

                    return businessObject;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("UserAccount::SelectByPrimaryKey::Error occured.", ex);
            }
            finally
            {             
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }

        /// <summary>
        /// Select all rescords
        /// </summary>
        /// <returns>list of UserAccount</returns>
        /// 
        public object GetLastReCordID()
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "Select UserID from UserAccount where UserID=IDENT_CURRENT('UserAccount')";
            sqlCommand.CommandType = CommandType.Text;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;
            try
            {

                MainConnection.Open();

                return sqlCommand.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw new Exception("Customer::Get Last User::Error occured.", ex);
            }
            finally
            {
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }
        public List<UserAccount> SelectAll()
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[UserAccount_SelectAll]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {
                
                MainConnection.Open();

                IDataReader dataReader = sqlCommand.ExecuteReader();

                return PopulateObjectsFromReader(dataReader);

            }
            catch (Exception ex)
            {                
                throw new Exception("UserAccount::SelectAll::Error occured.", ex);
            }
            finally
            {
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }
        public List<ELSmallUser> SelectAllUserName()
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "SELECT UserID,UserName FROM [UserAccount]";
            sqlCommand.CommandType = CommandType.Text;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

                MainConnection.Open();

                IDataReader dataReader = sqlCommand.ExecuteReader();

                return PopulateSmallObjectsFromReader(dataReader);

            }
            catch (Exception ex)
            {
                throw new Exception("UserAccount::SelectAll::Error occured.", ex);
            }
            finally
            {
                MainConnection.Close();
                sqlCommand.Dispose();
            }
        }

        /// <summary>
        /// Select records by field
        /// </summary>
        /// <param name="fieldName">name of field</param>
        /// <param name="value">value of field</param>
        /// <returns>list of UserAccount</returns>
        public List<UserAccount> SelectByField(string fieldName, object value)
        {

            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[UserAccount_SelectByField]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

                sqlCommand.Parameters.Add(new SqlParameter("@FieldName", fieldName));
                sqlCommand.Parameters.Add(new SqlParameter("@Value", value));

                MainConnection.Open();
                
                IDataReader dataReader = sqlCommand.ExecuteReader();

                return PopulateObjectsFromReader(dataReader);

            }
            catch (Exception ex)
            {
                throw new Exception("UserAccount::SelectByField::Error occured.", ex);
            }
            finally
            {

                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }

        /// <summary>
        /// Delete by primary key
        /// </summary>
        /// <param name="keys">primary keys</param>
        /// <returns>true for successfully deleted</returns>
        public bool Delete(UserAccountKeys keys)
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[UserAccount_DeleteByPrimaryKey]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

				sqlCommand.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, keys.UserID));


                MainConnection.Open();

                sqlCommand.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("UserAccount::DeleteByKey::Error occured.", ex);
            }
            finally
            {                
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }

        /// <summary>
        /// Delete records by field
        /// </summary>
        /// <param name="fieldName">name of field</param>
        /// <param name="value">value of field</param>
        /// <returns>true for successfully deleted</returns>
        public bool DeleteByField(string fieldName, object value)
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[UserAccount_DeleteByField]";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

                sqlCommand.Parameters.Add(new SqlParameter("@FieldName", fieldName));
                sqlCommand.Parameters.Add(new SqlParameter("@Value", value));
                
                MainConnection.Open();

                sqlCommand.ExecuteNonQuery();

                return true;

            }
            catch (Exception ex)
            {                
                throw new Exception("UserAccount::DeleteByField::Error occured.", ex);
            }
            finally
            {             
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Populate business object from data reader
        /// </summary>
        /// <param name="businessObject">business object</param>
        /// <param name="dataReader">data reader</param>
        internal void PopulateBusinessObjectFromReader(UserAccount businessObject, IDataReader dataReader)
        {


				businessObject.UserID = dataReader.GetInt32(dataReader.GetOrdinal(UserAccount.UserAccountFields.UserID.ToString()));

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.UserName.ToString())))
				{
					businessObject.UserName = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.UserName.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.Password.ToString())))
				{
					businessObject.Password = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.Password.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.Name.ToString())))
				{
					businessObject.Name = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.Name.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.Gender.ToString())))
				{
					businessObject.Gender = dataReader.GetBoolean(dataReader.GetOrdinal(UserAccount.UserAccountFields.Gender.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.Address.ToString())))
				{
					businessObject.Address = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.Address.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.District.ToString())))
				{
					businessObject.District = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.District.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.City.ToString())))
				{
					businessObject.City = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.City.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.Birthday.ToString())))
				{
					businessObject.Birthday = dataReader.GetDateTime(dataReader.GetOrdinal(UserAccount.UserAccountFields.Birthday.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.HomePhone.ToString())))
				{
					businessObject.HomePhone = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.HomePhone.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.CellPhone.ToString())))
				{
					businessObject.CellPhone = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.CellPhone.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.Email.ToString())))
				{
					businessObject.Email = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.Email.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.UserType.ToString())))
				{
					businessObject.UserType = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.UserType.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.Deleted.ToString())))
				{
					businessObject.Deleted = dataReader.GetBoolean(dataReader.GetOrdinal(UserAccount.UserAccountFields.Deleted.ToString()));
				}


        }
        internal void PopulateSmallBusinessObjectFromReader(ELSmallUser businessObject, IDataReader dataReader)
        {
            businessObject.UserID = dataReader.GetInt32(dataReader.GetOrdinal(UserAccount.UserAccountFields.UserID.ToString()));

            if (!dataReader.IsDBNull(dataReader.GetOrdinal(UserAccount.UserAccountFields.UserName.ToString())))
            {
                businessObject.UserName = dataReader.GetString(dataReader.GetOrdinal(UserAccount.UserAccountFields.UserName.ToString()));
            }
        }

        /// <summary>
        /// Populate business objects from the data reader
        /// </summary>
        /// <param name="dataReader">data reader</param>
        /// <returns>list of UserAccount</returns>
        internal List<UserAccount> PopulateObjectsFromReader(IDataReader dataReader)
        {

            List<UserAccount> list = new List<UserAccount>();

            while (dataReader.Read())
            {
                UserAccount businessObject = new UserAccount();
                PopulateBusinessObjectFromReader(businessObject, dataReader);
                list.Add(businessObject);
            }
            return list;

        }
        internal List<ELSmallUser> PopulateSmallObjectsFromReader(IDataReader dataReader)
        {

            List<ELSmallUser> list = new List<ELSmallUser>();

            while (dataReader.Read())
            {
                ELSmallUser businessObject = new ELSmallUser();
                PopulateSmallBusinessObjectFromReader(businessObject, dataReader);           
                list.Add(businessObject);
            }
            return list;

        }
        #endregion

	}
}
